<template>
    <div class="container-xl">
        <div class="row">
            <div class="colname col">
                <div class="type">
                    第
                    <span class="text-00c8a5">{{ index + 1 }}</span>
                    题 （{{
                        question.type === "多选" ? "不定项选择" : question.type
                    }}）
                </div>
            </div>
        </div>
        <div class="context question-html" v-html="question.context"></div>
        <div v-if="question.type === '判断'" class="row">
            <div class="col-6">
                <div class="select-p indexcorep dui">
                    <div class="text-center div" style="line-height: 120px" :span="24"
                        :class="data == true ? 'bg-primaryp' : ''" @click="panduan(true)">
                        <svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-dui" />
                        </svg>
                        正确
                    </div>
                </div>
            </div>
            <div class="col-6">
                <div class="select-p indexcorep cuo">
                    <el-row>
                        <el-col class="text-center div" style="line-height: 120px" @click="panduan(false)"
                            :class="data == false ? 'bg-primaryp' : ''">
                            <svg class="icon" aria-hidden="true">
                                <use xlink:href="#icon-cuowu" />
                            </svg>
                            错误
                        </el-col>
                    </el-row>
                </div>
            </div>
        </div>
        <div v-else-if="question.type === '单选'" class="row">
            <div class="col-6" v-for="(v, k) in option" :key="k">
                <div class="select-a indexcore" :class="data == ['A', 'B', 'C', 'D'][k] ? 'bg-primary1' : ''">
                    <el-container @click="xuanze(['A', 'B', 'C', 'D'][k])">
                        <el-aside width="30px" class="select-b">
                            <div v-text="['A', 'B', 'C', 'D'][k]"></div>
                        </el-aside>
                        <el-main class="select-main p-0">
                            <el-row>
                                <el-col class="position-relative p-2" style="min-heihgt: 60px" :span="24">
                                    <img class="el-img" v-if="image(v)" :src="v" />
                                    <span v-else class="question-html">{{ v }}</span>
                                </el-col>
                            </el-row>
                        </el-main>
                    </el-container>
                </div>
            </div>
        </div>
        <div v-else-if="question.type === '多选'" class="row">
            <div class="col-6" v-for="(v, k) in ['A', 'B', 'C', 'D']" :key="k">
                <div class="select-a indexcore" :class="Array.isArray(data) && data.includes(v) ? 'bg-primary1' : ''
                    ">
                    <el-container @click="xuanze(['A', 'B', 'C', 'D'][k], true)">
                        <el-aside width="30px" class="select-b">
                            <div v-text="['A', 'B', 'C', 'D'][k]"></div>
                        </el-aside>
                        <el-main class="p-0 select-main">
                            <el-row>
                                <el-col class="position-relative p-2" style="min-heihgt: 60px" :span="24">
                                    <img class="el-img" v-if="image(option[k])" :src="option[k]" />
                                    <span class="text-wrap text-truncate d-inline-block question-html" v-else>{{
                                        option[k] }}</span>
                                </el-col>
                            </el-row>
                        </el-main>
                    </el-container>
                </div>
            </div>
        </div>
        <div v-else-if="question.type === '填空'" class="form-group row justify-content-md-center">
            <label for="staticEmail" class="col-12 col-form-label text-center mb-3">空白处填入：</label>
            <div class="col-12 text-center">
                <input class="pl-3 form-control" id="staticEmail" v-model.lazy="data" placeholder="请在此处填写内容" />
            </div>
        </div>
    </div>
</template>
<script setup lang="ts">

import {
    computed,
    nextTick,
    onMounted,
    ref,
    toRefs,
    watch,
} from "vue";

import Studycodemodel from "../mysql_interface/studycodemodel";
import questionhtml from "../util/htmlstring";
import { isImage } from "../util/RegExp";

type P = {
    question: Studycodemodel.Imatchquestion
    index: number
}

const p = defineProps<P>()


const { question } = toRefs(p);

const option = computed(() => {
    if (question.value.answer) {
        return question.value.answer.options || [];
    }
    return [];
});

const data = ref<boolean | string | string[]>();
onMounted(() => {
    start();
});
function start() {
    nextTick(() => {
        questionhtml()
    });
}


watch(
    () => question.value,
    () => {
        start();
    }
);

function image(str: string) {
    return isImage.test(str);
}
function panduan(t: boolean) {
    data.value = t;
}

function xuanze(t: string, type: boolean) {
    if (type) {
        // 多选
        if (!Array.isArray(data.value)) {
            data.value = [];
        }
        const f = data.value.findIndex((v) => v === t);

        if (f !== -1) {
            data.value.splice(f, 1);
        } else {
            if (data.value.length >= 4) {
                data.value.splice(0, 1);
            }
            data.value.push(t);
        }
    } else {
        data.value = t;
    }
}


</script>
<style scoped>
.select-match {
    height: 90vh;
    margin-top: 0px;
    background-color: #fff;
    padding-top: 40px;
}

.colname {
    text-align: center;
    margin-bottom: 35px;
}

.colname .type {
    width: 220px;
    height: 42px;
    border: 1px solid var(--color-cheng);
    opacity: 1;
    border-radius: 28px;
    line-height: 40px;
    font-size: 16px;
    margin: 0 auto;
    font-weight: 400;
    color: #909399;
    opacity: 1;
}

.context {
    margin-bottom: 100px;
    background: #ffffff;
    box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.16);
    opacity: 1;
    border-radius: 24px;
    padding: 33px;
}

.bg-primary1 {
    color: #fff;
}

.foter {
    padding-top: 10px;
    padding-left: 20px;
    min-height: 60px;
    position: fixed;
    bottom: 0;
    width: 100vw;
    background-color: #fff;
    border-top: 1px solid #dfdfdf;
    margin-left: 0px;
    margin-right: 0px;
}

.select-a {
    width: 200px;
    min-height: 60px;
    margin-left: auto;
    margin-right: auto;
    border-radius: 28px;
    margin-bottom: 50px;
    /* transition: all s; */
    border: 1px solid var(--color-cheng);
}

.select-a .el-container {
    cursor: pointer;
}

.select-a .select-b {
    height: 100%;
    text-align: center;
    font-size: 24px;
    font-weight: bold;
    color: var(--color-cheng);
    opacity: 1;

    margin-left: 10px;
}

.select-b div {
    line-height: 60px;
}

.select-main {
    display: flex;
    font-size: 16px;
}

.position-relative {
    display: flex;
    align-items: center;
    overflow: hidden;
}

.select-main i {
    position: absolute;
    font-size: 60px;
    right: 20px;
    bottom: 20px;
}

.el-img {
    max-width: 100%;
    object-fit: cover;
}

.bg-primary1 {
    background-color: var(--color-cheng) !important;
    border-width: 0;
    color: #fff !important;
    font-weight: bold;
}

.bg-primary1 .select-b {
    color: #fff;
}

.indexcore:hover {
    background: var(--color-cheng3);
    /* color: var(--color-303133); */
    color: #fff;
    /* border-width: var(--color-cheng3); */
}

.form-control {
    width: 400px;
    margin: 0 auto;
}

.form-control:focus {
    color: #495057;
    background-color: #fff;
    border-color: #dcdfe6;
    outline: 0;
    box-shadow: none !important;
}

.select-p {
    width: 280px;
    height: 120px;
    opacity: 1;
    border-radius: 12px;
    margin: 0 auto;
    line-height: 120px;
    cursor: pointer;
    transition: all 0.3s;
    overflow: hidden;
}

.select-p .div {
    font-size: 24px;
}

.select-p .icon {
    font-size: 48px;
    transform: translateY(7px);
}

.dui {
    border: 1px solid var(--color-cheng);
}

.dui .icon {
    color: var(--color-cheng);
}

.cuo {
    border: 1px solid #f56c6c;
}

.cuo .icon {
    color: #f56c6c;
}

.dui:hover {
    background-color: #d8f3ee;
    border-width: 0px;
}

.bg-primaryp {
    background-color: #3ac2a2;
    color: #fff !important;
}

.bg-primaryp .icon {
    color: #fff !important;
}

.cuo:hover {
    background-color: #fde2e2;
    border-width: 0px;
}

.cuo .bg-primaryp {
    background-color: #f56c6c;
}
</style>